package com.yb.web.controller.model;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yb.framework.core.domain.AjaxObject;
import com.yb.framework.core.domain.PageResult;
import com.yb.framework.core.utils.StringUtil;
import com.yb.framework.log.annotation.Log;
import com.yb.framework.log.enums.OperateTypeEnum;
import com.yb.module.model.domain.ModelThing;
import com.yb.web.controller.base.BaseController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Controller
@RequestMapping("/model/thing")
@Api(tags = "实物管理")
public class ModelThingController extends BaseController {
	
	Logger logger = LoggerFactory.getLogger(ModelThingController.class.getName());
	
	private static String MODEL_THING = "model/thing/thing";
	private static String MODEL_THING_ADD = "model/thing/thing_add";
	private static String MODEL_THING_EDIT = "model/thing/thing_edit";
	private static String MODEL_NEO4J_CHART = "model/neo4j/chart";
	
	@GetMapping()
	@PreAuthorize("hasAuthority('model:thing')")
	public String main() {
		return MODEL_THING;
	}
	
	@GetMapping("/add_view")
	@PreAuthorize("hasAuthority('model:thing:add')")
	public String addView() {
		return MODEL_THING_ADD;
	}
	
	@GetMapping("/edit_view/{id}")
	@PreAuthorize("hasAuthority('model:thing:edit')")
	public String editView(@PathVariable("id") Long id, ModelMap model) {
		model.put("modelThing", modelThingService.getById(id));
		model.put("template", modelTemplateService.getById(modelThingService.getById(id).getTemplateId()));
		model.put("shapeIds", StringUtil.list2String(modelThingShapeService.getShapeIdsByThingId(id)));
		return MODEL_THING_EDIT;
	}
	
	@GetMapping("/chart/{id}")
	@PreAuthorize("hasAuthority('model:thing:chart')")
	public String chart(@PathVariable("id") Long id, ModelMap model) {
		model.put("label", "thing");
		model.put("nodeUuid", id);
		return MODEL_NEO4J_CHART;
	}
	
	/**
	 * 实物列表
	 */
	@PostMapping("/list")
	@PreAuthorize("hasAuthority('model:thing:list')")
	@ApiOperation(value="分页查询", notes="分页查询实物信息")
	public @ResponseBody PageResult list(ModelThing modelThing) {
		return getPageInfo(modelThingService.getPageByEntity(modelThing));
	}
	
	/**
	 * 新增实物
	 */
	@PostMapping("/add")
	@PreAuthorize("hasAuthority('model:thing:add')")
	@ApiOperation(value="新增实物", notes="新增实物信息")
	@Log(label = "新增实物", operateType = OperateTypeEnum.ADD)
	public @ResponseBody AjaxObject add(ModelThing modelThing) {
		return toAjax(modelThingService.insertThing(modelThing), "新增实物" + "[" + modelThing.getName() + "]");
	}
	
	/**
	 * 编辑实物
	 */
	@PostMapping("/edit")
	@PreAuthorize("hasAuthority('model:thing:edit')")
	@ApiOperation(value="编辑实物", notes="编辑实物信息")
	@Log(label = "编辑实物", operateType = OperateTypeEnum.EDIT)
	public @ResponseBody AjaxObject edit(ModelThing modelThing) {
		return toAjax(modelThingService.updateThing(modelThing), "编辑实物" + "[" + modelThing.getName() + "]");
	}
	
	/**
	 * 删除实物
	 */
	@PostMapping("/delete")
	@PreAuthorize("hasAuthority('model:thing:delete')")
	@ApiOperation(value="删除实物", notes="批量删除实物信息")
	@Log(label = "删除实物", operateType = OperateTypeEnum.DELETE)
	public @ResponseBody AjaxObject delete(@RequestParam("ids") String ids) {
		List<String> modelThingNames = modelThingService.getNamesByIds(StringUtil.str2LongArray(ids));
		return toAjax(modelThingService.deleteThing(StringUtil.str2LongArray(ids)), "批量删除实物" + modelThingNames);
	}
	
	/**
	 * 实物下拉框
	 */
	@RequestMapping("/combo")
	public @ResponseBody List<ModelThing> combo() {
		return modelThingService.getAll();
	}
}